package tests;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList;

import org.junit.Test;

import utils.AmbigousPath;
import utils.Node;
import utils.Path;
import utils.TreeUtils;

public class ContextTest {

	@Test
	public final void testMergePathsIntoTree() throws CloneNotSupportedException {

		TreeConstants.init();
		Node configuration = TreeConstants.configuration;
		Node rule = TreeConstants.rule2;

		ArrayList<Node> leaves = TreeUtils.getLeaves(rule);
		System.out.println("Leaves: ");
		for(Node path:leaves)
		{
			System.out.println("	leaf: " + path);
		}		

		ArrayList<Node> intermediateNodes = TreeUtils.getIntermediateNodes(rule);

		System.out.println("INodes: ");
		for(Node path:intermediateNodes)
		{
			System.out.println("	inode: " + path);
		}		
		

		ArrayList<Path> leavesPaths = TreeUtils.getPaths(configuration, leaves);

		System.out.println("Leaves paths: (" + leavesPaths.size() + ")");
		for(Path path:leavesPaths)
		{
			System.out.println("	leaf path: " + path);
		}		
		
		ArrayList<Path> intermediatePaths = TreeUtils.getPaths(configuration,
				intermediateNodes);

		
		System.out.println("Intermediate paths: (" + intermediatePaths.size() + ")");
		for(Path path:intermediatePaths)
		{
			System.out.println("	leaf path: " + path);
		}		

		
		ArrayList<Path> pathContexts = TreeUtils.getPathContext(leavesPaths,
				intermediatePaths, rule.label);

		System.out.println("Context paths: (" + pathContexts.size() + ")");
		for(Path path:pathContexts)
		{
			System.out.println("	context path: " + path);
		}		
		
		
		ArrayList<AmbigousPath> ambigousPaths = TreeUtils
				.detectAmbiguity(pathContexts);
		Node result = null;
		if (ambigousPaths == null) 
		{
			result = TreeUtils.merge(pathContexts);
			TreeUtils.printTree(result, "");
		}

		assertTrue(result != null);
	}

}
